﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;

namespace NovelReader
{
    public class XMLNovelManage
    {
        /// <summary>
        /// 根据XML文件信息
        /// 
        /// 找出所有未正常更新记录，更新
        /// </summary>
        /// <param name="novel"></param>
        public void DownLoadLinks(Novel novel)
        {
            string novelPath = Path.Combine(CommonDef.BaseDir, novel.NovelName, novel.NovelName + ".xml");
            XElement xDoc = XElement.Load(novelPath);

            var links = (from t in xDoc.Descendants("Link")
                         where t.Attribute("IsUpdated").Value == "0"
                         select t).ToList();

            foreach (var link in links)
            {
                string srcUrl = link.Attribute("SiteUrl").Value + link.Attribute("Href").Value;
                string dstPath = Path.Combine(CommonDef.BaseDir, novel.NovelName, link.Attribute("Href").Value);

                if (CommonDef.DownLoadLink(srcUrl, dstPath))
                {
                    link.Attribute("IsUpdated").Value = "1";
                }
            }

            xDoc.Save(novelPath);
        }


        /// <summary>
        /// 通过XML文件来存储小说的信息
        /// </summary>
        /// <param name="novel"></param>
        /// <returns></returns>
        public void CheckNovelFromXml(Novel novel)
        {
            XElement eleChapter = null;
            XElement eleLink = null;

            string novelPath = Path.Combine(CommonDef.BaseDir, novel.NovelName, novel.NovelName + ".xml");
            if (!File.Exists(novelPath))
            {
                // 如果小说不存在。那么目录肯定也是不存在的。需要创建
                Directory.CreateDirectory(Path.Combine(CommonDef.BaseDir, novel.NovelName));

                XElement xRoot = new XElement("Novel",
                    new XAttribute("Name", novel.NovelName));
                xRoot.Save(novelPath);
            }

            XElement xDoc = XElement.Load(novelPath);

            // 遍历所有章节
            foreach (var chap in novel)
            {

                eleChapter = (from t in xDoc.Descendants("Chapter")
                              where t.Attribute("Name").Value == chap.ChapterName
                              && t.Attribute("ID").Value == chap.ChapterID.ToString()
                              select t).FirstOrDefault();

                if (eleChapter == null)
                {
                    eleChapter = new XElement("Chapter",
                        new XAttribute("Name", chap.ChapterName),
                        new XAttribute("ID", chap.ChapterID));
                    xDoc.Add(eleChapter);
                }

                // 开始添加Link信息
                foreach (var link in chap)
                {
                    eleLink = (from t in eleChapter.Descendants("Link")
                               where t.Attribute("Description").Value == link.Description
                               && t.Attribute("ID").Value == link.LinkID.ToString()
                               select t).FirstOrDefault();
                    if (eleLink == null)
                    {
                        eleLink = new XElement("Link",
                            new XAttribute("Title", link.Title),
                            new XAttribute("ID", link.LinkID),
                            new XAttribute("Href", link.Href),
                            new XAttribute("Description", link.Description),
                            new XAttribute("SiteUrl", link.SiteUrl),
                            new XAttribute("IsUpdated", "0"),
                            new XAttribute("UpdateTime", link.UpdateTime)
                            );
                        eleChapter.Add(eleLink);
                    }

                }
            }

            xDoc.Save(novelPath);
        }

    }
}
